﻿var oMediaCapture;
var profile;
var captureInitSettings;
var deviceList = new Array();
var recordState = false;
var storageFile;

(function () {

    function errorHandler(e) {
        var message = new Windows.UI.Popups.MessageDialog(e.message);
        message.showAsync().then(function () {
            WinJS.Navigation.back();
        });
    }

    function errorHandler(e) {
        var message = new Windows.UI.Popups.MessageDialog(e.message);
        message.showAsync().then(function () {
            WinJS.Navigation.navigate("pages/home/home.html")
        });
    }

    // Begin initialization.
    function initialization() {
        //document.getElementById("message").innerHTML = "Initialization started.";
        enumerateCameras();
    }

    // Identify available cameras.
    function enumerateCameras() {
        var deviceInfo = Windows.Devices.Enumeration.DeviceInformation;
        deviceInfo.findAllAsync(Windows.Devices.Enumeration.DeviceClass.audioCapture).then(function (devices) {
            // Add the devices to deviceList
            if (devices.length > 0) {
                for (var i = 0; i < devices.length; i++) {
                    deviceList.push(devices[i]);
                }

                initCaptureSettings();
                initMediaCapture();
                //document.getElementById("message").innerHTML = "Initialization complete.";
            }
            else {
                var message = new Windows.UI.Popups.MessageDialog("No microphone device is found ");
                message.showAsync();
            }
        }, errorHandler);
    }

    // Initialize the MediaCaptureInitialzationSettings.
    function initCaptureSettings() {
        captureInitSettings = null;
        captureInitSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
        captureInitSettings.audioDeviceId = "";
        captureInitSettings.streamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.audio;
        captureInitSettings.realTimeModeEnabled = true;
        if (deviceList.length > 0)
            captureInitSettings.audioDeviceId = deviceList[0].id;
    }

    // Create a profile.
    function createProfile() {
        profile = Windows.Media.MediaProperties.MediaEncodingProfile.createMp3(
            Windows.Media.MediaProperties.AudioEncodingQuality.high);
    }

    // Create and initialze the MediaCapture object.
    function initMediaCapture() {
        oMediaCapture = null;
        oMediaCapture = new Windows.Media.Capture.MediaCapture();
        oMediaCapture.initializeAsync(captureInitSettings).then(function (result) {
            createProfile();
        }, errorHandler);
    }

    // Start the audio capture.
    function startMediaCaptureSession(keyName) {
        var roamingFolder = Windows.Storage.ApplicationData.current.roamingFolder;

        roamingFolder.createFileAsync(keyName + ".mp3", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (newFile) {
            storageFile = newFile;
            oMediaCapture.startRecordToStorageFileAsync(profile, storageFile).then(function (result) {
            }, errorHandler);
        });
    }

    // Stop the audio capture.
    function stopMediaCaptureSession() {
        return new WinJS.Promise.as(oMediaCapture.stopRecordAsync().then(function (result) {
        }, errorHandler));
    }

    WinJS.Namespace.define("Record", {
        initRecord: initialization,
        startRecord: startMediaCaptureSession,
        stopRecord: stopMediaCaptureSession
    });

}());